import numpy as np
from sklearn.metrics import mean_squared_error, mean_absolute_error, r2_score

def evaluate_model(actual, predictions):
    """
    评估模型性能
    
    参数:
        actual: 实际值
        predictions: 预测值
        
    返回:
        包含各种评估指标的字典
    """
    # 移除NaN值
    mask = ~np.isnan(predictions)
    actual_valid = actual[mask]
    pred_valid = predictions[mask]
    
    # 计算评估指标
    mse = mean_squared_error(actual_valid, pred_valid)
    rmse = np.sqrt(mse)
    mae = mean_absolute_error(actual_valid, pred_valid)
    r2 = r2_score(actual_valid, pred_valid)
    
    return {
        'MSE': mse,
        'RMSE': rmse,
        'MAE': mae,
        'R2': r2
    }

def print_evaluation_metrics(metrics):
    """
    打印评估指标
    
    参数:
        metrics: 包含评估指标的字典
    """
    print("模型评估指标:")
    print(f"均方误差 (MSE): {metrics['MSE']:.4f}")
    print(f"均方根误差 (RMSE): {metrics['RMSE']:.4f}")
    print(f"平均绝对误差 (MAE): {metrics['MAE']:.4f}")
    print(f"决定系数 (R²): {metrics['R2']:.4f}")